import { _decorator, Component, director, Node, ProgressBar } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ExpWnd')
export class ExpWnd extends Component {
    private static instance: ExpWnd = null; // 单例实例

    @property(ProgressBar)
    private expBar: ProgressBar = null; // 经验条组件

    @property(Node)
    private selectWnd: Node = null; // 选择窗口

    private currentExp: number = 0; // 当前经验值
    private maxExp: number = 100; // 升级所需经验值
    private level: number = 1; // 当前等级

    // 获取单例实例
    public static getInstance(): ExpWnd {
        return ExpWnd.instance;
    }

    protected onLoad(): void {
        if (ExpWnd.instance === null) {
            ExpWnd.instance = this;
        } else {
            this.destroy();
            return;
        }
    }

    start() {
        this.updateExpBar();
    }

    // 增加经验值
    public addExp(exp: number) {
        this.currentExp += exp;
        if (this.currentExp >= this.maxExp) {
            this.levelUp();
        }
        this.updateExpBar();
    }

    // 升级逻辑
    private levelUp() {
        this.level++;
        this.currentExp -= this.maxExp;
        this.maxExp = Math.floor(this.maxExp * 1.5); // 每次升级所需经验增加50%
        console.log(`升级了！当前等级：${this.level}`);
        if (this.level < 4) {
            this.showSelectWnd();
        }
    }

    // 更新经验条显示
    private updateExpBar() {
        if (this.expBar) {
            this.expBar.progress = this.currentExp / this.maxExp;
        }
    }

    update(deltaTime: number) {
        // 可以在这里添加一些经验条动画效果
    }

    private showSelectWnd() {
        this.selectWnd.active = true;
        director.pause();
    }
}
